package com.ebuy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.dao.NewsMapper;
import com.ebuy.dao.NoticeMapper;
import com.ebuy.dao.ProductMapper;
import com.ebuy.dao.SrdMapper;
import com.ebuy.dao.UserMapper;
import com.ebuy.model.News;
import com.ebuy.model.Notice;
import com.ebuy.model.Product;
import com.ebuy.model.Srd;
import com.ebuy.model.User;

@Controller
@RequestMapping("/user")
public class UserControl {
	@Autowired
	private SrdMapper srdMapper;
	@Autowired
	private NewsMapper newsMapper;
	@Autowired
	private NoticeMapper noticeMapper;
	@Autowired
	private ProductMapper productmapper;
	@Autowired
	private UserMapper userMapper;
	//一.--------------进入主页面------------------
	@RequestMapping("/index")
	public String index(Model model,HttpServletRequest req){
		//传入左边导航
		System.out.println("进入主页");
		List<Srd> srd=srdMapper.list();
		System.out.println(srd.get(2));
		model.addAttribute("srd", srd);
		//传入新闻
		List<News> news=newsMapper.selectall();
		model.addAttribute("news", news);
		System.out.println(news.get(1));
		//传入公告
		List<Notice> notice=noticeMapper.list();
		model.addAttribute("notice", notice);
		//传入特价
		List<Product> product=productmapper.selectSpePrice();
		System.out.println(product.get(0));
		model.addAttribute("product", product);
		//添加历史浏览信息cookies 需要改
		String strIds = "";
		Cookie[] cks = req.getCookies();
		if(cks!=null && cks.length>0){
			for(Cookie ck : cks){
				if("ids".equals(ck.getName())){
					strIds = ck.getValue();
					break;
				}
			}
		}
		if(!strIds.equals("")){
			List<Product> browsePros = new ArrayList<Product>();
			String[] ids = strIds.split("-");
			for(int i=ids.length-1;i>=0&i>=ids.length-3;i--){
				Product browsePro =  productmapper.selectByPrimaryKey(Integer.valueOf(ids[i]));
				if(browsePro.getpName().length()>15){
					browsePro.setpName(browsePro.getpName().substring(0,15)+"...");
				}
				browsePros.add(browsePro);
			}
			model.addAttribute("browsePros", browsePros);
		}
		
		return "page/index";
	}
	//二.--------------注册页面--------------------------
	@RequestMapping(value="/reg",method=RequestMethod.GET)
	public String reg(){
		return "page/register";
	}
	@RequestMapping(value="/reg",method=RequestMethod.POST)
	public String reg(Model model,User user,String uPassword2){
		String bool="";
		User u=userMapper.selectbyname(user.getuUsername());
		if(u!=null){
			bool="用户已存在请重新注册";
			model.addAttribute("bool", bool);
			return "page/register";	
		}
		if(user.getuUsername()==null){
			bool="请填写完整信息";
			model.addAttribute("bool", bool);
			return "page/register";	
		}
		if(user.getuPassword()==null){
			bool="请填写完整信息";
			model.addAttribute("bool", bool);
			return "page/register";	
		}
		if(user.getuNickname()==null){
			bool="请填写完整信息";
			model.addAttribute("bool", bool);
			return "page/register";	
		}
		if(!uPassword2.equals(user.getuPassword())){
			bool="密码不一致";
			model.addAttribute("bool", bool);
			return "page/register";
		}
		userMapper.insertSelective(user);
		return "page/login";
	}
	//三.--------------登录页面------------------------------
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
      return "page/login";
}   
	//与数据库做比较，调数据库，做判断，成功后返回result
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(Model model,User user,HttpSession s){
		String bool="";
		User u=userMapper.selectbyname(user.getuUsername());
		if(user.getuUsername()==null){
			bool="用户或密码不能为空";
			model.addAttribute("bool", bool);
			return "page/login";	
		}
		if(user.getuPassword()==null){
			bool="用户或密码不能为空";
			model.addAttribute("bool", bool);
			return "page/login";	
		}
		if(u==null){
			bool="用户不存在";
			model.addAttribute("bool", bool);
			return "page/login";	
		}
		if(!u.getuUsername().equals(user.getuUsername())){
			bool="用户名或密码不正确";
			model.addAttribute("bool", bool);
			return "page/login";
		}
		if(!u.getuPassword().equals(user.getuPassword())){
			bool="用户名或密码不正确";
			model.addAttribute("bool", bool);
			return "page/login";
		}
      s.setAttribute("User", user);
      return "redirect:/user/index";
}
	//四.--------------留言板块-------------------------------
	@RequestMapping("/guest")
	public String guest(){
		return "page/guestbook";
	}

	//六.-------------购物页面----------------------
	//传入商品信息,
	@RequestMapping(value="/shop",method=RequestMethod.GET)
	public String shop(Model model,HttpServletRequest res){
		String id=res.getParameter("id");
		Product pro=productmapper.selectByPrimaryKey(Integer.valueOf(id));
		model.addAttribute("pro", pro);
		return "page/shopping";
	}
	//点击购买
	@RequestMapping(value="/shop",method=RequestMethod.POST)
	public String shop(Model model){
		return "page/shopping-result";
				
	}

}